package com.zzyk.microCloud.api.controller;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageInfo;
import com.zzyk.main.configuration.exception.UserException;
import com.zzyk.main.model.pojo.MaintenanceInfo;
import com.zzyk.main.model.pojo.SysUser;
import com.zzyk.main.model.vo.DateAndString;
import com.zzyk.main.model.vo.MaintenanceView;
import com.zzyk.main.model.vo.Message;
import com.zzyk.main.model.vo.WxUser;
import com.zzyk.main.service.DeviceService;
import com.zzyk.main.service.ISysUserService;
import com.zzyk.main.service.MaintenanceInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/api/maintenance")
public class ApiMaintenanceController extends ApiBaseController {

    @Autowired
    private ISysUserService userService;
    @Autowired
    private MaintenanceInfoService maintenanceInfoService;
    @Autowired
    private DeviceService deviceService;



    @GetMapping("/getInfoList")
    public Message getInfoList(String dateTime) throws UserException {
        SysUser sysUser = getUser().getSysUser();
        setPage();
        PageInfo<MaintenanceInfo> info = new PageInfo<>(maintenanceInfoService.findInfoList(dateTime, sysUser.getEnterprise(), sysUser.getDeviceList()));
        return Message.success()
                .addData("data", info.getList())
                .addData("total", info.getTotal())
                .addData("pages", info.getPages());
    }

    /**
     * 设备故障维修排行
     *
     * @param dateTime
     * @return
     * @throws UserException
     */
    @GetMapping("/getFailureRanking")
    public List<MaintenanceView> getFailureRanking(String dateTime) throws UserException {
        SysUser sysUser = getUser().getSysUser();
        return maintenanceInfoService.findFailureRanking(dateTime, sysUser.getEnterprise(), sysUser.getDeviceList());
    }

    /**
     * 查询日期范围内所有的维保数据 默认30天
     *
     * @param dateTime
     * @return
     * @throws UserException
     */
    @GetMapping("/getDateRangeMaintenance")
    public List<MaintenanceInfo> getDateRangeMaintenance(String dateTime) throws UserException {
        SysUser sysUser = getUser().getSysUser();
        Integer[] deviceList = sysUser.getDeviceList();
        return maintenanceInfoService.findDateRangeMaintenance(dateTime, sysUser.getEnterprise(), deviceList);
    }

    /**
     * 获取结论里部分数据
     *
     * @param dateTime
     * @return
     * @throws UserException
     */
    @GetMapping("/getConclusion")
    public Message getConclusion(String dateTime) throws UserException {
        SysUser sysUser = getUser().getSysUser();
        return maintenanceInfoService.findConclusion(dateTime, sysUser.getEnterprise(), sysUser.getDeviceList());
    }
    /**
     * 根据设备ID查询维保情况维保倒计时，上次维保时间
     *
     * @param id
     * @return
     */
    @GetMapping("/getMaintenanceInfoByDId")
    public DateAndString getMaintenanceInfoByDId(Integer id) {
        return maintenanceInfoService.selectMaintenanceInfoByDId(id);
    }

    @RequestMapping("list")
    public Message list() throws UserException {
        SysUser sysUser = getUser().getSysUser();
        return Message.success()
                .addData("data", deviceService.getMaintenanceList(null, sysUser.getEnterprise(), sysUser.getDeviceList()))
                .addData("extension", deviceService.getExtensionMaintenanceInfo(null, sysUser.getEnterprise(), sysUser.getDeviceList()));
    }

    @RequestMapping("getMaintenanceInfoByParamTo")
    public Message getInfoListTo(@RequestParam(value = "type", defaultValue = "1") Integer type,
                                 @RequestParam(value = "beginTime", required = false) String beginTime,
                                 @RequestParam(value = "endTime", required = false) String endTime) throws UserException {
        SysUser sysUser = getUser().getSysUser();
        setPage();
        PageInfo<MaintenanceInfo> info = new PageInfo<>(maintenanceInfoService.selectAllByParam(beginTime, endTime, type, sysUser.getEnterprise(), null, null, null, sysUser.getDeviceList()));
        return Message.success()
                .addData("data", info.getList())
                .addData("total", info.getTotal())
                .addData("pages", info.getPages());
    }

    @RequestMapping("getMaintenanceInfoByParam")
    public Message getInfoList(@RequestParam(value = "type", defaultValue = "1") Integer type,
                               @RequestParam(value = "beginTime", required = false) String beginTime,
                               @RequestParam(value = "endTime", required = false) String endTime,
                               Integer deviceType, String name) throws UserException {

        WxUser user = getUser();
        SysUser sysUser = userService.getUser(user.getUserId());
        setPage();
        PageInfo<MaintenanceInfo> info = new PageInfo<>(maintenanceInfoService.selectAllByParam(beginTime, endTime, type, sysUser.getEnterprise(), null, name, deviceType, sysUser.getDeviceList()));
        return Message.success()
                .addData("data", info.getList())
                .addData("total", info.getTotal())
                .addData("pages", info.getPages());
    }


    @GetMapping("/countMaintenanceInfoByParam")
    public Message countMaintenanceInfoByParam(@RequestParam(value = "type", defaultValue = "1") Integer type,
                                               @RequestParam(value = "beginTime", required = false) String beginTime,
                                               @RequestParam(value = "endTime", required = false) String endTime,
                                               Integer deviceType, String name) throws UserException {
        SysUser user = getUser().getSysUser();
        return Message.success()
                .addData("data", maintenanceInfoService.countMaintenanceInfoByParamApi(beginTime, endTime, type, user.getEnterprise(), null, name, deviceType, user.getDeviceList()));

    }


    @RequestMapping("saveOrUpdateMaintenance")
    public Message saveOrUpdateMaintenance(MaintenanceInfo maintenanceInfo) throws IOException, UserException {
        WxUser user = userService.getWxUserByOpenId(maintenanceInfo.getOpenId());
        if (maintenanceInfo.getMethodType() == 1) {
            maintenanceInfo.setUpdateTime(DateUtil.parseDateTime(DateUtil.now()));
            maintenanceInfoService.updateByPrimaryKeyAndFileUpload(maintenanceInfo, user.getUserId());
            return Message.success();
        }
        maintenanceInfoService.insertMaintenanceInfo(maintenanceInfo, user.getUserId());
        return Message.success();
    }
}
